package com.lyf.viewapp.cards;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import com.lyf.viewapp.R;
import com.lyf.viewapp.base.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 探探卡片
 */
public class SwipeCardsActivity extends BaseActivity {

    @BindView(R.id.swipe_cards_view)
    SwipeFlingAdapterView flingContainer;

    private ArrayList<String> al;
    private ArrayList<String> basic;
    private ArrayList<String> removeData;

    private ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_cards);
        ButterKnife.bind(this);

        al = new ArrayList<>();
        basic = new ArrayList<>();
        removeData = new ArrayList<>();
        basic.add("php1");
        basic.add("c2");
        basic.add("python3");
        basic.add("java4");
        basic.add("html5");
        basic.add("c++6");
        basic.add("css7");
        basic.add("javascript8");
        al.addAll(basic);

        arrayAdapter = new ArrayAdapter<>(this, R.layout.swipe_cards_item, R.id.helloText, al);

        flingContainer.setAdapter(arrayAdapter);
        flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                Log.d("LIST", "removed object!");
                removeData.add(al.remove(0));
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onLeftCardExit(Object dataObject) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
                makeToast(SwipeCardsActivity.this, "Left!");
            }

            @Override
            public void onRightCardExit(Object dataObject) {
                makeToast(SwipeCardsActivity.this, "Right!");
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                // Ask for more data here
                al.addAll(removeData);
                removeData.clear();
                arrayAdapter.notifyDataSetChanged();
                Log.d("LIST", "notified");
                //al.add("XML ".concat(String.valueOf(i)));
                //i++;
            }

            @Override
            public void onScroll(float scrollProgressPercent) {
                View view = flingContainer.getSelectedView();
                view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
                view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
            }
        });


        // Optionally add an OnItemClickListener
        flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
            @Override
            public void onItemClicked(int itemPosition, Object dataObject) {
                makeToast(SwipeCardsActivity.this, "Clicked!");
            }
        });

    }

    static void makeToast(Context ctx, String s) {
        Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show();
    }


    @OnClick(R.id.right)
    public void right() {
        /**
         * Trigger the right event manually.
         */
        flingContainer.getTopCardListener().selectRight();
    }

    @OnClick(R.id.left)
    public void left() {
        flingContainer.getTopCardListener().selectLeft();
    }

    @OnClick(R.id.previous)
    public void previous() {
        if (removeData.size() > 0) {
            al.add(0, removeData.remove(removeData.size() - 1));
            arrayAdapter.notifyDataSetChanged();
        } else {
            makeToast(SwipeCardsActivity.this, "已经是第一项了!");
        }
    }
}
